
package com.microcore.pdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * Created by mabb on 15-4-8.
 */
public class PDFHelper {

    // 创建文档
    public static void createDocument(List<Element> contextList, String filePath) {
        try {
            Document doc = new Document(PageSize.A4, 50, 50, 50, 50);
            PdfWriter.getInstance(doc, new FileOutputStream(filePath));

            doc.addAuthor("故障诊断软件");
            doc.open();
            for (Element eContext : contextList) {
                try {
                    if (null == eContext) {
                        continue;
                    }
                    doc.add(eContext);
                } catch (DocumentException e) {
                    continue;
                }
            }

            doc.close();
        } catch (FileNotFoundException e1) {
        } catch (DocumentException e1) {
        }

    }

    // 创建标题
    public static Element productMainTitleContent(String titleMsg) {
        Font f = getChineseFont();
        f.setColor(BaseColor.BLACK);
        f.setSize(20);
        f.setStyle(Font.BOLD);

        Paragraph p = new Paragraph(titleMsg, f);
        p.setAlignment(Element.ALIGN_CENTER);
        return p;
    }

    // 一级标题
    public static Element productFirstTitleContent(String data) {
        Font f = getChineseFont();
        f.setColor(BaseColor.BLACK);
        f.setSize(16);

        Paragraph p = new Paragraph(" " + data, f);
        p.setAlignment(Element.ALIGN_LEFT);
        return p;
    }

    // 二级标题
    public static Element productSecondTitleContent(String data) {
        Font f = getChineseFont();
        f.setColor(BaseColor.BLACK);
        f.setSize(14);

        Paragraph p = new Paragraph("    " + data, f);
        p.setAlignment(Element.ALIGN_LEFT);
        return p;
    }

    // 三级标题
    public static Element productThirdTitleContent(String data) {
        Font f = getChineseFont();
        f.setColor(BaseColor.BLACK);
        f.setSize(12);
        Paragraph p = new Paragraph("         " + data, f);
        p.setAlignment(Element.ALIGN_LEFT);
        return p;
    }

    // 普通文本
    public static Element productTextContent(String data) {
        Font f = getChineseFont();
        f.setColor(BaseColor.BLACK);
        f.setSize(16);

        Paragraph p = new Paragraph("      " + data, f);
        p.setAlignment(Element.ALIGN_LEFT);
        p.setIndentationLeft(2);
        return p;
    }

    //图片
    public static Element productPicture(String picFilePath) {
        Image image = null;
        try {
            image = Image.getInstance(picFilePath);
            //合理压缩，h>w，按w压缩，否则按w压缩
            int percent = getPercent(image.getHeight(), image.getWidth());
            //表示是原来图像的比例
            image.scalePercent(percent);
        } catch (BadElementException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }

    /**
     * 第一种解决方案
     * 在不改变图片形状的同时，判断，如果h>w，则按h压缩，否则在w>h或w=h的情况下，按宽度压缩
     *
     * @param h
     * @param w
     * @return
     */

    private static int getPercent(float h, float w) {
        int p = 0;
        float p2 = 0.0f;
        p2 = 450 / w * 100;
        p = Math.round(p2);
        return p;
    }

    // 表格
    public static Element productExcelContents(String[] tcTitles, String[][] tcContents) {
        Font f = getChineseFont();
        f.setSize(12);
        int colNum = tcTitles.length;
        PdfPTable table = new PdfPTable(colNum);
        table.setHorizontalAlignment(Element.ALIGN_LEFT);//设置表格的对齐方式

        table.setSpacingBefore(15f);
        table.getDefaultCell().setBorder(1);

        for (int i = 0; i < tcTitles.length; i++) {
            String colTitle = tcTitles[i];
            table.addCell(getPDFCell(colTitle, f));
        }

        for (int i = 0; i < tcContents.length; i++) {
            String[] row = tcContents[i];
            for (String rowText : row) {
                table.addCell(getPDFCell(rowText, f));
            }
        }
        return table;
    }

    //获取指定内容与字体的单元格
    private static PdfPCell getPDFCell(String string, Font font) {
        //创建单元格对象，将内容与字体放入段落中作为单元格内容
        PdfPCell cell = new PdfPCell(new Paragraph(string, font));

        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

        //设置最小单元格高度
        cell.setMinimumHeight(20);
        return cell;
    }

    public static Font getChineseFont() {
        //中文字体,解决中文不能显示问题
        BaseFont bfChinese = null;
        try {
            bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new Font(bfChinese);
    }


}
